/**
 * 根据当前位置，查询在附近的卡片
 */
const async = require('async');
module.exports = {
    open:false,
    description: '获取附近卡片',
    handler(req, res, next) {
        let doc = req.query;
        let filter = doc.filter || {};
        let page = doc.page;
        let project = doc.project || {
            picPath: 1,
            soundPath: 1,
            title: 1,
            latitude: 1,
            longitude: 1,
            place: 1,
            cardKindID: 1,
            authorID: 1,
            headPicPath: 1,
            nickname: 1,
            careID: 1,
            likeID: 1,
            commentID: 1,
            checkState: 1,
            createTime: 1,
            lnglat: 1,
            distance: 1
        };
        let latitude = filter.latitude;//纬度
        let longitude = filter.longitude;//经度
        let search = filter.search;
        let sort = {"distance": 1};
        let skip = 0;
        let limit = 10;
        if (page) {
            if (page.size) {
                limit = page.size;
                if (page.index > 1) {
                    skip = (page.index - 1) * page.size;
                }
            }
        }

        req.mongo.collection("card").indexExists("lnglat_2d", (e, r) => {//indexExists方法用于判定2d索引是否存在，返回值是boolean类型
            if (e) return res.err.MongoError(e);
            if (!r) {
                req.mongo.collection("card").createIndex({"lnglat": "2d"}, (err, re) => {
                    if (err) return res.err.MongoError(err);
                })
            }
            async.auto([
                    callback => req.mongo.collection("card").aggregate([
                        {
                            $geoNear: {
                                "near": [longitude, latitude],
                                "distanceField": "distance",//经过$geoNear管道操作后，会产生一个新的属性“distance”并添加到card集合中，其值与距当前点的距离相关
                                "maxDistance": 5000 / 100 / (6371 * 3.14159265359 / 180),//数值"5"代表半径5KM范围之内
                                "spherical": true
                            }
                        },
                        {$match: {"checkState": 1, "title": {"$regex": search}}},
                        {$project: project},
                        {$sort: sort}
                    ]).toArray(callback)
                ],
                (err, results) => {
                    if (err) return res.err.mongoError(err);
                    let card = results[0] || [];
                    if (!page) {
                        return res.reply(card);
                    }

                    let count = card.length;
                    if (err) return res.err.mongoError(err);
                    page.size = page.size || 10;
                    page.index = page.index || 1;
                    page.count = count;
                    page.pageCount = Math.ceil(page.count / page.size);
                    if (count <= 0) return res.reply(page);
                    page.hasPrevious = page.index > 1;
                    page.hasNext = page.index < page.pageCount;
                    page.index = page.pageCount < page.index ? page.pageCount : page.index;
                    page.items = card.slice(skip, skip + limit);
                    return res.reply({code: 0, message: "查询成功", data: page});
                }
            )
        })
    }
};
